﻿#include"snake.h"

void SetPos(short x, short y)
{
	//获得标准输出设备的句柄
	HANDLE houtput = NULL;
	houtput = GetStdHandle(STD_OUTPUT_HANDLE);

	//定位关闭的位置
	COORD pos = { x,y };

	//修改光标的位置
	SetConsoleCursorPosition(houtput, pos);
}

//欢迎界面的打印
void WelcomeToGame()
{

	SetPos(40,14);
	wprintf(L"欢迎来到贪吃蛇小游戏\n");
	SetPos(42, 20);
	system("pause");
	system("cls");
	SetPos(25, 14);
	wprintf(L"用↑.↓.←.→来控制蛇的移动方向，按F3加速、F4减速\n");
	SetPos(30, 15);
	wprintf(L"加速能够获得更高的分数");
	system("pause");
	system("cls");


	SetPos(42, 20);
	system("pause");
	system("cls");

}

//创建地图
void CreatMap()
{
	//上
	int i = 0;

	for (i = 0; i < 29; i++)
	{
		wprintf(L"%lc", WALL); //●
	}

	//下
	SetPos(0, 26);
	for (i = 0; i < 29; i++)
	{
		wprintf(L"%lc", WALL); //●
	}

	//左
	for (i = 0; i < 26; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", WALL); //●
	}

	//右
	for (i = 0; i < 26; i++)
	{
		SetPos(56, i);
		wprintf(L"%lc", WALL); //●
	}
}

//创建食物
void CreatFood(pSnake ps)
{
	int x = 0;
	int y = 0;

	//生成食物坐标
	again:
	do
	{
		x = (rand() % 53) + 2;
		y = (rand() % 25) + 1;
	} while (x % 2 != 0);
	
	//x和y不能和蛇冲突

	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		if (x == cur->x && y == cur->y)
		{
			goto again;
		}
		cur = cur->next;
	}

	//创建食物的节点
	pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));

	if (pFood == NULL)
	{
		perror("CreatFood::malloc");
		return;
	}
	pFood->x = x;
	pFood->y = y;
	pFood->next = NULL;

	SetPos(x, y);
	wprintf(L"%lc", L'菜');

	ps->_pFood = pFood;
}

//初始化蛇
void InitSnake(pSnake ps)
{
	int i = 0;
	pSnakeNode cur = NULL;

	for (i = 0; i < 5; i++)
	{
		cur=(pSnakeNode)malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("InitSnake()::malloc;");
			return;
		}
		cur->next = NULL;
		cur->x = POS_X + 2 * i;
		cur->y = POS_Y;

		//头插
		if (ps->_pSnake == NULL)  //空链表
		{
			ps->_pSnake = cur;
		}
		else //非空链表
		{
			cur->next = ps->_pSnake;
			ps->_pSnake = cur;
		}
	}

	cur = ps->_pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}

	//设置贪吃蛇的属性
	ps->_dir = RIGHT;  //默认向右
	ps->_score = 0;
	ps->_food_weight = 10;
	ps->_sleep_time = 200;//单位为毫秒
	ps->_status = OK;

}

void GameStart(pSnake ps)
{
	//0.光标隐藏
	system("mode con cols=100 lines=30");
	system("title 贪吃蛇");
	HANDLE houtput = NULL;
	houtput = GetStdHandle(STD_OUTPUT_HANDLE);

	//定义一个光标信息的结构体
	CONSOLE_CURSOR_INFO cursor_info = { 0 };
	//获取和houtput句柄相关的控制台上的光标信息，存放在cursor_info
	GetConsoleCursorInfo(houtput, &cursor_info);
	//修改光标的可见度
	cursor_info.bVisible = false;
	SetConsoleCursorInfo(houtput, &cursor_info);

	//1.打印环境界面
	//2.功能介绍 
	WelcomeToGame();

	//3.绘制地图
	CreatMap();


	//4.创建蛇
	InitSnake(ps);

	//5.创建食物
	CreatFood(ps);

}

void GameEnd(pSnake ps)
{
	SetPos(24, 12);
	switch(ps->_status)
	{
	case END_NORMAL:
		printf("你主动结束了游戏!\n");
		break;

	case KILL_BY_SELF:
		printf("你咬到了自己，寄!\n");
		break;

	case KILL_BY_WALL:
		printf("你撞到墙上，创死了!\n");
		break;
	}
	SetPos(0, 27);

	//释放蛇身

	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		pSnakeNode del = cur;
		cur = cur->next;
		free(del);
	}
}

//帮助信息
void PrintfHelpInfo()
{
	SetPos(64, 10);
	wprintf(L"%ls", L"不能穿墙，不能咬自己");
	SetPos(64, 11);
	wprintf(L"%ls", L"用↑.↓.←.→来控制蛇的移动方向");	
	SetPos(64, 12);
	wprintf(L"%ls", L"按F3加速、F4减速");	
	SetPos(64, 13);
	wprintf(L"%ls", L"@hmcjn,偷吃🐍游戏");	
	SetPos(64, 14);
	wprintf(L"%ls", L"按ESC正常退出，按空格暂停游戏");
	SetPos(64, 20);
	wprintf(L"%ls", L"菜就多练，输不起就别玩");

}

//1表示按过，0表示没按过
#define KEY_PRESS(vk) ((GetAsyncKeyState(vk)&1)?1:0)

//暂停
void Pause()
{
	while (1)
	{
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
		Sleep(200);
	}

}

//检查下一个坐标是不是食物
int NextIsFood(pSnakeNode pn, pSnake ps)
{
	return (ps->_pFood->x == pn->x && ps->_pFood->y == pn->y);
}

//下一个位置是食物就吃掉
void EatFood(pSnakeNode pn, pSnake ps)
{
	//头插法
	ps->_pFood->next = ps->_pSnake;
	ps->_pSnake = ps->_pFood;

	//释放下一个位置节点pNextNode
	free(pn);
	pn = NULL;

	pSnakeNode cur=ps->_pSnake;
	//打印
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	ps->_score += ps->_food_weight;

	//重新创建食物
	CreatFood(ps);
}

//不是食物
void NoFood(pSnakeNode pn, pSnake ps)
{
	//头插法
	pn->next = ps->_pSnake;
	ps->_pSnake = pn;

	pSnakeNode cur = ps->_pSnake;
	while (cur->next->next != NULL)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	//最后一个节点打印成空格
	SetPos(cur->next->x, cur->next->y);
	printf("  ");
	//释放最后一个节点
	free(cur->next);
	//置空倒数第二个节点
	cur->next = NULL;
}

//检测蛇是否撞墙
void KillByWall(pSnake ps)
{
	if (ps->_pSnake->x == 0 || ps->_pSnake->x == 56 || ps->_pSnake->y == 0
		|| ps->_pSnake->y == 26)
	{
		ps->_status = KILL_BY_WALL;
	}
}

//检测蛇咬到自己
void KillBySelf(pSnake ps)
{
	pSnakeNode cur = ps->_pSnake->next;
	while (cur)
	{
		if (cur->x == ps->_pSnake->x && cur->y == ps->_pSnake->y)
		{
			ps->_status = KILL_BY_SELF;
			break;
		}
		cur = cur->next;
	}
}

//蛇移动
void SnakeMove(pSnake ps)
{
	
	//创建蛇要去的下一个节点
	pSnakeNode pNextNode = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pNextNode == NULL)
	{
		perror("SnakeMove::malloc");
		return;
	}



	switch (ps->_dir)
	{
	case UP:
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y = ps->_pSnake->y - 1;
		break;

	case DOWN:
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y = ps->_pSnake->y + 1;
		break;

	case LEFT:		
		pNextNode->x = ps->_pSnake->x - 2;
		pNextNode->y = ps->_pSnake->y;
		break;

	case RIGHT:
		pNextNode->x = ps->_pSnake->x + 2;
		pNextNode->y = ps->_pSnake->y;
		break;
	}
	//检查下一个坐标是不是食物
	if (NextIsFood(pNextNode, ps))
	{
		EatFood(pNextNode, ps);
	}
	else
	{
		//不是食物
		NoFood(pNextNode, ps);
	}

	//检测蛇是否撞墙
	KillByWall(ps);

	//检测蛇咬到自己
	KillBySelf(ps);

}

//游戏运行的逻辑
void GameRun(pSnake ps)
{
	//帮助信息
	PrintfHelpInfo();

	do
	{
		//打印总分数和食物的分值
		SetPos(64, 16);
		printf("总分数:%d\n", ps->_score);
		SetPos(64, 17);
		printf("当前食物分数:%2d\n", ps->_food_weight);

		if (KEY_PRESS(VK_UP) && ps->_dir != DOWN)
		{
			ps->_dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->_dir != UP)
		{
			ps->_dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_dir != RIGHT)
		{
			ps->_dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_dir != LEFT)
		{
			ps->_dir = RIGHT;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			//暂停
			Pause();
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			//正常退出游戏
			ps->_status = END_NORMAL;
		}
		else if (KEY_PRESS(VK_F3))
		{
			//加速
			if (ps->_sleep_time > 80)
			{
				ps->_sleep_time -= 30;
				ps->_food_weight += 2;
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			//减速
			if (ps->_food_weight > 2)
			{
				ps->_sleep_time += 30;
				ps->_food_weight -= 2;
			}
		}

		//蛇移动
		SnakeMove(ps);

		Sleep(ps->_sleep_time);

	} while (ps->_status == OK);

}